﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoachingOrganizer.Database.DataAccess;
using CoachingOrganizer.Toolkit.Events;
using CoachingOrganizer.ClientInfrastructure.ServicesEntities.LocalStorage.Tools.EmotionsPack;

namespace CoachingOrganizer.Database.DataAccessLayer.Tools.EmotionsPack
{
    public class EmotionsPackDAL : ToolDALBase, IEmotionsPackInfo
    {
        private readonly List<EmotionTraceItemDAL> _emotionTraceItems = new List<EmotionTraceItemDAL>();
        private EmotionsPackDA _da;
        public string _name;

        public List<EmotionTraceItemDAL> EmotionTraceItems
        {
            get { return _emotionTraceItems; }
        }

        public IEnumerable<IEmotionTraceItemInfo> TraceItemsInfo
        {
            get { return _emotionTraceItems; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public EmotionsPackDAL(DbManagerBase dbManager)
            : base(dbManager)
        {

        }

        public override object CreateNewDA(GoalDA goal)
        {
            return _da = new EmotionsPackDA
            {
                GoalId = goal.Id,
                Name = _name
            };
        }

        public override ActionResult Save()
        {
            _da.Name = _name;
            return _dbManager.SafeRun(() => _dbManager.Context.InsertOrReplace(_da));
        }

        public bool Load(int id)
        {
            EmotionsPackDA emotionsPack = _dbManager.Context.Find<EmotionsPackDA>(da => da.Id == id);
            if (emotionsPack == null)
                return false;
            Load(emotionsPack);
            return true;
        }

        public void Load(EmotionsPackDA emotionsPack)
        {
            _da = emotionsPack;
            _name = emotionsPack.Name;
            LoadEmotionTraceItems();
        }

        public override ActionResult Remove()
        {
            if (_da != null)
                return _dbManager.SafeRun(() => _dbManager.Context.Delete<EmotionsPackDA>(_da.Id));
            return ActionResult.GetErrorResult(new KeyNotFoundException());
        }

        public EmotionTraceItemDAL CreateNewEmotionTraceItem()
        {
            return new EmotionTraceItemDAL(_dbManager);
        }

        public void AddEmotionTraceItem(EmotionTraceItemDAL emotionTraceItem)
        {
            _emotionTraceItems.Add(emotionTraceItem);
            _dbManager.Context.Insert(emotionTraceItem.CreateNewDA(_da));
        }

        public ActionResult<IEmotionTraceItemInfo> AddNewTraceItem(Action<IEmotionTraceItemInfo> initCallback)
        {
            EmotionTraceItemDAL newTraceItem = CreateNewEmotionTraceItem();
            if (initCallback != null)
                initCallback(newTraceItem);
            ActionResult addResult = _dbManager.SafeRun(() => AddEmotionTraceItem(newTraceItem));
            return addResult.IsValid ? ActionResult<IEmotionTraceItemInfo>.GetValidResult(newTraceItem) : ActionResult<IEmotionTraceItemInfo>.GetErrorResult(addResult);
        }

        public void LoadEmotionTraceItems()
        {
            foreach (EmotionTraceItemDA emotionTraceItemDA in _dbManager.Context.Table<EmotionTraceItemDA>().Where(g => g.EmotionsPackId == _da.Id).ToArray())
            {
                EmotionTraceItemDAL emotionTraceItem = new EmotionTraceItemDAL(_dbManager);
                emotionTraceItem.Load(emotionTraceItemDA);
                _emotionTraceItems.Add(emotionTraceItem);
            }
        }

    }
}